# Contributor: Bart Ribbers <bribbers@disroot.org>
# Maintainer: Oliver Smith <ollieparanoid@postmarketos.org>
pkgname=calamares
pkgver=3.2.61
pkgrel=5
# armhf blocked by qt5-qtdeclarative
# ppc64le, s390x, and riscv64 blocked by qt5-qtwebengine
arch="all !armhf !ppc64le !s390x !riscv64"
url="https://calamares.io/"
pkgdesc="Distribution-independent installer framework"
license="GPL-3.0-or-later AND LGPL-2.1-only"
depends="
	ckbcomp
	musl-locales
	os-prober
	"
depends_dev="
	extra-cmake-modules
	kcoreaddons5-dev
	qt5-qttools-dev
	yaml-cpp-dev
	"
makedepends="$depends_dev
	kconfig5-dev
	kcrash5-dev
	ki18n5-dev
	kpackage5-dev
	kparts5-dev
	kpmcore-dev
	kservice5-dev
	kwidgetsaddons5-dev
	libatasmart-dev
	libpwquality-dev
	plasma-framework5-dev
	polkit-qt-dev
	qt5-qtbase-dev
	qt5-qtdeclarative-dev
	qt5-qtsvg-dev
	qt5-qtwebengine-dev
	"
# required for several modules
makedepends="$makedepends
	boost-dev
	boost-python3
	parted-dev
	python3-dev
	"
checkdepends="
	py3-toml
	tzdata
	xvfb-run
	"
source="https://github.com/calamares/calamares/releases/download/v$pkgver/calamares-$pkgver.tar.gz
	modules-load.conf
	"
subpackages="$pkgname-dev $pkgname-doc $pkgname-lang"

# Modules to build:
# https://github.com/calamares/calamares/tree/master/src/modules
_modules="
	bootloader
	displaymanager
	finished
	fsresizer
	fstab
	grubcfg
	hostinfo
	hwclock
	interactiveterminal
	keyboard
	keyboardq
	locale
	localeq
	luksbootkeyfile
	luksopenswaphookcfg
	machineid
	mkinitfs
	mount
	netinstall
	networkcfg
	notesqml
	oemid
	packagechooser
	packages
	partition
	plasmalnf
	plymouthcfg
	preservefiles
	rawfs
	removeuser
	services-openrc
	shellprocess
	summary
	umount
	unpackfs
	users
	webview
	welcome
	welcomeq
	"

for i in $_modules; do
	subpackages="$pkgname-mod-$i:_module $subpackages"
done

# Check if $1 is in $_modules
is_module_enabled() {
	local i
	for i in $_modules; do
		[ "$i" = "$1" ] && return 0
	done
	return 1
}

prepare() {
	default_prepare

	local i
	cd "$builddir/src/modules"

	# Fill $_skip_modules, list disabled modules
	msg "Disabled modules:"
	for i in *; do
		if ! [ -d "$i" ] || is_module_enabled "$i"; then
			continue
		fi
		_skip_modules="$_skip_modules $i"
		echo " - $i"
	done

	# List enabled modules
	msg "Enabled modules:"
	for i in $_modules; do
		echo " - $i"
	done
}

build() {
	cmake -B build \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DINSTALL_CONFIG=ON \
		-DSKIP_MODULES="$_skip_modules"
	cmake --build build
}

check() {
	cd build
	# libcalamaresnetworktest requires network access
	# machineidtest fails to detect random poolsize
	# userstest always expects "x8664X" cpu name
	CTEST_OUTPUT_ON_FAILURE=TRUE xvfb-run ctest -E "libcalamaresnetworktest|machineidtest|userstest" -j1
}

_module() {
	local module=${subpkgname##calamares-mod-}
	local path="usr/lib/calamares/modules"

	mkdir -p "$subpkgdir/$path"
	mv "$pkgdir/$path/$module" "$subpkgdir/$path/$module"

	# Module-specific dependencies
	case "$module" in
		displaymanager)
			depends="$depends py3-toml"
			;;
		unpackfs)
			depends="$depends rsync"
			install="$install $subpkgname.post-install"
			install -Dm644 "$srcdir"/modules-load.conf \
				"$subpkgdir"/usr/lib/modules-load.d/calamares.conf
			;;
		mkinitfs)
			depends="$depends mkinitfs"
			;;
		locale)
			depends="$depends tzdata"
			;;
	esac
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

sha512sums="
04292f1ef9ee478dfb6cc044471f5fd6ac6e64b40e80b730cb7d38dc7ed56bae4e3a40ea483e9167502896e4ce190cecf7acea98394f1de22139912a84b937ab  calamares-3.2.61.tar.gz
c56ad3b92901abdb41cffaeceeff9a6c8dae3882aacf9a4b654a8898f1e26eeadda3ac3ac799b127b176cc31d397652f27a07bcdfbf7f4ede3c6826d08f7bd7d  modules-load.conf
"
